package com.xjscrm.console.chat.core.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerConversationReadMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RReadMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatReadMsgHandler extends ChatBaseMsgHandler<RReadMsg> {

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;
    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Override
    public SResultMsg doHandle(RReadMsg content, ChannelHandlerContext ctx) {


        Long merId = content.getMerId();
        Long staffId = content.getStaffId();


        try {
            for (Long talkSessionId : content.getTalkSessionId()) {

                ChatTalkSession talkSession = chatTalkSessionService.getById(merId, talkSessionId);
                if (Objects.isNull(talkSession)) {
                    throw new BusinessException("已读消息失败: 会话不存在");
                }
                ChatTalkSession update = new ChatTalkSession();

                update.setId(talkSession.getId());
                update.setMerId(talkSession.getMerId());
                update.setLastReadTime((int) (System.currentTimeMillis() / 1000L));
                update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

                chatTalkSessionService.update(update);

                MicaTriggerConversationReadMsg conversationReadMsg = new MicaTriggerConversationReadMsg();
                conversationReadMsg.setWxId(talkSession.getConversationId());
                conversationReadMsg.setMessageId(IdUtil.getSnowflakeNextId());
                // 构建trigger对象
                MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(conversationReadMsg, MicaTriggerType.TRIGGER_CONVERSATION_READ, item -> {
                    item.setReqId(conversationReadMsg.getMessageId());
                    item.setDeviceId(talkSession.getDeviceId());
                    item.setDeviceType(talkSession.getDeviceType());
                });
                sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);
            }
            return SResultMsg.success(content, "已读回话成功");
        } catch (Exception e) {
            log.error("已读回话失败", e);
            throw new BusinessException("已读回话失败");
        }
    }

    @Override
    public boolean checkMsg(RReadMsg baseMsg) {
        return super.checkMsg(baseMsg) || ObjectUtils.isEmpty(baseMsg.getTalkSessionId());
    }

    @Override
    public Class<RReadMsg> msgClass() {
        return RReadMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_read_msg;
    }

}
